package com.cqust.wdm.emsboot.controller;


import com.cqust.wdm.emsboot.service.IMailService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/mail")
public class MailController {

    @Autowired
    private IMailService mailService;

    @Autowired
    private TemplateEngine templateEngine;

    @ApiOperation("发送邮件")
    @PostMapping("/send")
    public Map<String, Object> send(String subject, String content) {

        return mailService.send(subject, content);
    }

    @PostMapping("/test")
    public String test(String subject, String content) {

        // 定义模板数据
        Context context = new Context();
        HashMap<String, Object> paramMap = new HashMap<>(16);
        paramMap.put("candidate", "bravo1988");
        paramMap.put("sex", 1);
        paramMap.put("publisher", "马云");
        paramMap.put("jobName", "Java高级开发工程师(saas方向)");
        paramMap.put("icon", "https://pic4.zhimg.com/v2-1907eb21be63d35b077e6ed3cbcbfe13_xll.jpg");
        paramMap.put("school", "清华大学");
        paramMap.put("major", "计算机科学与技术");
        paramMap.put("education", "本科");
        paramMap.put("status", "已离职");
        paramMap.put("expectJob", "软件工程师");
        paramMap.put("expectCity", "杭州");
        paramMap.put("salaryBegin", 10);
        paramMap.put("salaryEnd", 12);
        paramMap.put("appendixUrl", "https://wise-job.oss-cn-zhangjiakou.aliyuncs.com/wiseJob/1601035929262.pdf");
        paramMap.put("appendixName", "bravo1988_Java.pdf");
        context.setVariables(paramMap);
        String emailContent = templateEngine.process("emailTemplate", context);

        mailService.sendHtmlMail("1679167928@qq.com", "主题：这是模板邮件", emailContent);
        return emailContent;
    }

    @RequestMapping("/wordtest")
    public String wordtest() {


        HashMap<String, Object> paramMap = new HashMap<>(16);
        paramMap.put("r_content", "情人节，情人节的起源有多个版本");
        paramMap.put("r_issue", "这是遇到的问题～～～～～～～～～～～");

        // 定义模板数据
        Context context = new Context();
        context.setVariables(paramMap);
        String emailContent = templateEngine.process("templates.xml", context);

        File file = new File("test.docx");
        // 写入doc文件
        try {
            BufferedOutputStream bos = new BufferedOutputStream(Files.newOutputStream(file.toPath()));
            bos.write(emailContent.getBytes());
            bos.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
//        mailService.sendHtmlMail("1679167928@qq.com","主题：这是模板邮件",emailContent);
        return emailContent;
    }
}
